package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.UserKit;
import cn.stylefeng.guns.sys.modular.enums.UserTypes;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinbader.xiaole.common.entity.ProductWarningInfo;
import com.sinbader.xiaole.common.mapper.ProductWarningInfoMapper;
import com.sinbader.xiaole.common.model.params.ProductWarningInfoParam;
import com.sinbader.xiaole.common.model.result.ProductWarningInfoResult;
import com.sinbader.xiaole.common.service.ShopWarnConfigService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品预警信息表 服务实现类
 * </p>
 *
 * @author LuoYong
 * @since 2020-04-23
 */
@Service
public class ShopWarnConfigServiceImpl extends ServiceImpl<ProductWarningInfoMapper, ProductWarningInfo> implements ShopWarnConfigService {

    @Override
    public void add(ProductWarningInfoParam param){
        ProductWarningInfo entity = getEntity(param);
        User user = UserKit.getUser();
        Long shopId = 0L;
        if (user.getType().equals(UserTypes.Shop)){
            shopId = user.getOrgId();
        }else {
            shopId = param.getOrgId();
        }
        entity.setShopId(shopId);
        entity.setCreateTime(new Date());
        entity.setCreateUser(user.getUserId());
        this.save(entity);
    }

    @Override
    public void delete(ProductWarningInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ProductWarningInfoParam param){
        ProductWarningInfo oldEntity = getOldEntity(param);
        ProductWarningInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdateTime(new Date());
        newEntity.setUpdateUser(UserKit.getUserId());
        this.updateById(newEntity);
    }

    @Override
    public ProductWarningInfoResult findBySpec(ProductWarningInfoParam param){
        return null;
    }

    @Override
    public List<ProductWarningInfoResult> findListBySpec(ProductWarningInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ProductWarningInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ProductWarningInfoParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ProductWarningInfo getOldEntity(ProductWarningInfoParam param) {
        return this.getById(getKey(param));
    }

    private ProductWarningInfo getEntity(ProductWarningInfoParam param) {
        ProductWarningInfo entity = new ProductWarningInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
